#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./paas/app/utils.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
from django.conf import settings
from account.http import http_get
from app.models import App, AppTags
from app.constants import VCS_TYPE_VALID_VALUES
from common.log import logger
from common.constants import APP_CODE_CHECK_PATTERN, APP_CODE_CHECK_MSG, GIT_URL_CHECK_PATTERN, SVN_URL_CHENK_PATTREN

def validate_app_code(app_code):
    """
    检查app_code是否合法
    """
    if not app_code:
        return (False, u'应用ID不能为空')
    code_len = len(app_code)
    if code_len < 3:
        return (False, u'应用ID长度不能少于3个字符')
    if code_len > 16:
        return (False, u'应用ID长度不能超过16个字符')
    if not APP_CODE_CHECK_PATTERN.match(app_code):
        return (False, APP_CODE_CHECK_MSG)
    is_exists = App.objects.filter(code=app_code).exists()
    if is_exists:
        return (False, u'应用 ID[%s]已存在' % app_code)
    return (
     True, u'校验通过')


def validate_app_name(name, old_name):
    """
    校验app名称
    """
    if len(name) > 20:
        return (False, u'应用名称长度不能超过20个字符')
    if old_name:
        is_exists = App.objects.filter(name=name).exclude(name=old_name).exists()
    else:
        is_exists = App.objects.filter(name=name).exists()
    if is_exists:
        return (False, u'应用名称[%s]已存在' % name)
    return (True, u'校验通过')


def validate_vcs_url(vcs_type, vcs_url):
    """
    校验地址
    """
    try:
        vcs_type = int(vcs_type)
    except:
        return (
         False, u'代码仓库[%s]不合法' % vcs_type)

    if vcs_type not in VCS_TYPE_VALID_VALUES:
        return (False, u'代码仓库[%s]不合法' % vcs_type)
    if str(vcs_type) == '0':
        cur_pattern = GIT_URL_CHECK_PATTERN if 1 else SVN_URL_CHENK_PATTREN
        return (cur_pattern.match(vcs_url) or False, u'请填写正确的仓库地址')
    return (
     True, u'校验通过')


def validate_app_tags(app_tags):
    tag = AppTags.objects.filter(code=app_tags)
    if tag:
        return (True, u'校验通过', tag[0])
    return (
     False, u'标签不存在', None)
    return


def get_all_users(bk_token):
    """
    获取所有用户的信息
    """
    param = {'bk_token': bk_token}
    if settings.LOGIN_DOMAIN:
        get_user_url = 'http://%s/login/accounts/get_all_user/' % settings.LOGIN_DOMAIN
    else:
        get_user_url = '%s/login/accounts/get_all_user/' % settings.LOGIN_HOST
    result, resp = http_get(get_user_url, param)
    resp = resp if result and resp else {}
    if result and resp:
        ret = resp.get('result', False) if 1 else False
        ret or logger.error(u'请求平台接口获取用户信息失败：%s' % resp.get('message', ''))
        return (
         False, [])
    return (True, resp.get('data', []))
